(function waitForVueAndPatchCheckcode() {
    const MAX_RETRY = 20;
    let count = 0;

    const tryPatch = () => {
        count++;

        const root = document.querySelector('#app')?.__vue__;
        if (!root || !root.$children?.length) {
            if (count < MAX_RETRY) return setTimeout(tryPatch, 500);
            console.warn("[hook] Vue root not found");
            return;
        }

        const loginComp = root.$children.find(c => c.$options?.name === 'login');
        if (!loginComp) {
            if (count < MAX_RETRY) return setTimeout(tryPatch, 500);
            console.warn("[hook] login component not found");
            return;
        }

        // ✅ patch validator
        if (loginComp.loginRules?.checkcode?.[0]) {
            console.log("[hook] Found login component, patching checkcode validator");

            loginComp.loginRules.checkcode[0].validator = function (_, __, cb) {
                cb(); // 无条件通过
            };

            // ✅ 预填验证码字段
            loginComp.loginForm.checkcode = 'xxxx';

            console.log("[hook] checkcode validator has been bypassed");
        } else {
            console.warn("[hook] checkcode validator not found");
        }

        // ✅ 移除验证码相关 DOM（el-input--suffix）
        const checkcodeInputWrapper = document.querySelector(
            '.el-input.el-input--prefix.el-input--suffix'
        );
        if (checkcodeInputWrapper) {
            checkcodeInputWrapper.remove();
            console.log("[hook] 验证码输入框已从页面移除");
        } else {
            console.warn("[hook] 没有找到验证码输入框 DOM");
        }
    };

    tryPatch();
})();
